package com.answer.service.scoring;

import com.answer.common.ErrorCode;
import com.answer.entity.App;
import com.answer.entity.UserAnswer;
import com.answer.exception.BusinessException;
import com.answer.model.enums.AppScoringStrategyEnum;
import com.answer.model.enums.AppTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评分策略上下文
 */
@Service
public class ScoringStrategyContext {

    /**
     * 自定义分数评分策略
     */
    @Resource
    private CustomScoreScoringStrategy customScoreScoringStrategy;

    /**
     * 自定义测试评分策略
     */
    @Resource
    private CustomTestScoringStrategy customTestScoringStrategy;

    @Resource
    private AiTestScoringStrategy aiTestScoringStrategy;

    /**
     * 评分
     * 根据不同的应用类型和评分策略，选择合适的评分策略执行评分操作
     *
     * @param choiceList 用户的选择列表
     * @param app        应用信息
     * @return 用户评分结果
     * @throws Exception 业务或系统异常
     */
    public UserAnswer doScore(List<String> choiceList, App app) throws Exception {
        // 根据应用类型获取对应的枚举
        AppTypeEnum appTypeEnum = AppTypeEnum.getEnumByValue(app.getAppType());
        // 根据评分策略获取对应的枚举
        AppScoringStrategyEnum appScoringStrategyEnum = AppScoringStrategyEnum.getEnumByValue(app.getScoringStrategy());
        // 如果应用类型或评分策略为空，则抛出业务异常
        if (appTypeEnum == null || appScoringStrategyEnum == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
        }
        // 根据不同的应用类别和评分策略，选择对应的策略执行
        switch (appTypeEnum) {
            case SCORE:
                // 根据评分策略选择执行的策略
                switch (appScoringStrategyEnum) {
                    case CUSTOM:
                        // 执行自定义分数评分策略
                        return customScoreScoringStrategy.doScore(choiceList, app);
                    case AI:
                        // 预留：执行AI评分策略
                        break;
                }
                break;
            case TEST:
                // 根据评分策略选择执行的策略
                return switch (appScoringStrategyEnum) {
                    case CUSTOM ->
                        // 执行自定义测试评分策略
                            customTestScoringStrategy.doScore(choiceList, app);
                    case AI ->
                        // 预留：执行AI评分策略
                            aiTestScoringStrategy.doScore(choiceList, app);
                };
        }
        // 如果没有匹配的策略，则抛出业务异常
        throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
    }
}
